import java.util.ArrayList;
import java.util.List;

public class PrimeNumberFinder {
    public static void main(String[] args) {
        int limit = 20000;
        List<Integer> primes = findPrimes(limit);
        printPrimes(primes, 5);
    }
    public static List<Integer> findPrimes(int limit) {
        List<Integer> primes = new ArrayList<>();
        if (limit >= 2) {
            primes.add(2);
        }
        for (int i = 3; i <= limit; i += 2) {
            if (isPrime(i)) {
                primes.add(i);
            }
        }
        return primes;
    }
    public static boolean isPrime(int number) {
        if (number < 2) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(number); i++) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }
    public static void printPrimes(List<Integer> primes, int countPerLine) {
        int count = 0;
        for (int prime : primes) {
            System.out.printf("%-7d", prime);
            count++;
            if (count % countPerLine == 0) {
                System.out.println();
            }
        }
        if (count % countPerLine != 0) {
            System.out.println();
        }
    }
}
